package com.spring.oaassist.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spera.shopoa.util.ShopOaSessionUtil;
import com.spera.shopoa.util.ShopOaSessionUtil.OaUser;
import com.spring.oaassist.web.model.BusinessDaily;
import com.spring.oaassist.web.model.CoursePlan;
import com.spring.oaassist.web.model.SalePlan;
import com.spring.oaassist.web.service.ReportService;
import com.spring.security.core.annotation.Log;
import com.spring.security.core.annotation.OaUserPermission;
import com.spring.security.web.model.BaseResult;
import com.spring.tools.MyTools;

@Controller
@RequestMapping(value = "/oaReport")

public class ReportController {
	
	@Resource
	private ReportService reportService;
	
	
//	@OaUserPermission(value="/oaReport/coursePlan")	
	@RequestMapping(value = "/coursePlanPage")
	public ModelAndView coursestudents(HttpServletRequest request) {
		return new ModelAndView("main/oareport/courseplan");
	}
	
	//1.8.2 课程计划 完成情况，排名 

	@RequestMapping(value = "/coursePlan")
    @ResponseBody
    public Object coursePlan(String page,String pageSize,String type,
    		String beginTime,String endTime,HttpServletRequest request){
		
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		String dept_id =oaUser.getUser_dept_id();
		String kids_park_id=oaUser.getKids_park_id();
		
		
		System.out.println(dept_id);		
		
        HashMap<String, Object> parm = new HashMap<String, Object>();
        HashMap<String, Object> retD = new HashMap<String, Object>();
        
        HashMap hmc=MyTools.checkIsNotNull(beginTime,endTime,page,pageSize,type);
        if(hmc!=null){
			
			retD.put("total", 0);
	        retD.put("data", new ArrayList());
	        return retD;
		}
        
        //parm.put("dept_id",dept_id);
        parm.put("kids_parkID",kids_park_id);
    	parm.put("beginTime",beginTime);
    	parm.put("endTime",endTime);
    	
    	String ptype="";
    	String format="";
    	
    	
    	if(type.equals("month")){
    		ptype="1";
    		format="%Y-%m";
    	}
    	if(type.equals("year")){
    		ptype="3";
    		format="%Y";
    	}
    	
    	
    	parm.put("type",ptype);
    	parm.put("format",format);
    	

    	System.out.println(parm.toString());
    	
    	
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(pageSize));
        
        List<CoursePlan> list =reportService.coursePlan(parm);
        
        //设置排名
        int index=1;
        String time="";
        for(CoursePlan cp:list){
        	String cptime=cp.getTime();
        	if(!time.equals(cptime)){
        		time=cptime;
        		index=1;
        	}
        	cp.setIndex(index);
        	index++;
        	
        }
        
        PageInfo pageObj = new PageInfo(list);
        retD.put("total", pageObj.getTotal());
        retD.put("data", list);
        return retD;
    }

	
	
	//课程计划实际
//	@OaUserPermission(value="/oaReport/saleplanpage")
	@RequestMapping(value = "/saleplanpage")
	public ModelAndView salePlanPage(HttpServletRequest request) {
		return new ModelAndView("main/oareport/saleplan");
	}
	
	
	@RequestMapping(value = "/salePlan")
    @ResponseBody
    public Object salePlan(String page,String pageSize,String type,
    		String beginTime,String endTime,HttpServletRequest request){
		

		
        HashMap<String, Object> parm = new HashMap<String, Object>();
        HashMap<String, Object> retD = new HashMap<String, Object>();
        
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		String dept_id =oaUser.getUser_dept_id();
        
        
        HashMap hmc=MyTools.checkIsNotNull(beginTime,endTime,page,pageSize,type);
        if(hmc!=null){
			
			retD.put("total", 0);
	        retD.put("data", new ArrayList());
	        return retD;
		}
        
        parm.put("dept_id",dept_id);
    	parm.put("beginTime",beginTime);
    	parm.put("endTime",endTime);
    	
    	String ptype="";
    	String format="";
    	
    	
    	if(type.equals("month")){
    		ptype="1";
    		format="%Y-%m";
    	}
    	if(type.equals("year")){
    		ptype="3";
    		format="%Y";
    	}
    	
    	
    	parm.put("type",ptype);
    	parm.put("format",format);
    	
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(pageSize));
        
        List<SalePlan> list =reportService.salePlan(parm);
        
        
        //设置排名
        int index=1;
        String time="";
        for(SalePlan cp:list){
        	String cptime=cp.getTime();
        	if(!time.equals(cptime)){
        		time=cptime;
        		index=1;
        	}
        	cp.setIndex(index);
        	index++;
        	
        }
        
        
        System.out.println("salePlan"+list.size());

        
        PageInfo pageObj = new PageInfo(list);
        retD.put("total", pageObj.getTotal());
        retD.put("data", list);
        return retD;
    }

	//营业日报表
	@OaUserPermission(value="/oaReport/businessDailyPage")
	@RequestMapping(value = "/businessDailyPage")
	public ModelAndView businessDaily(HttpServletRequest request) {
		return new ModelAndView("main/oareport/businessdaily");
	}
	

	@RequestMapping(value = "/businessDaily")
    @ResponseBody
    public Object businessDaily(String page,String pageSize,
    		String beginTime,String endTime,HttpServletRequest request){
		HashMap hmc=MyTools.checkIsNotNull(beginTime,endTime,page,pageSize);
		if(hmc!=null){
			return null;
		}
		
        HashMap<String, Object> parm = new HashMap<String, Object>();
        HashMap<String, Object> retD = new HashMap<String, Object>();
        
    	parm.put("beginTime",beginTime);
    	parm.put("endTime",endTime);
    	
    	
    	System.out.println(parm.toString());
    	
    	
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(pageSize));
        
        List<BusinessDaily> list =reportService.businessDaily(parm);
        
        System.out.println(list.toString());
        for(BusinessDaily mp:list){
        	//System.out.println(mp.toString());
        }
        
        PageInfo pageObj = new PageInfo(list);
        retD.put("total", pageObj.getTotal());
        retD.put("data", list);
        return retD;
    }
	
	@RequestMapping(value = "/total")
    public ModelAndView baseType(HttpServletRequest request){
		return new ModelAndView("main/oareport/total");
    }
	
	@RequestMapping(value = "/income")
	@ResponseBody
	public Object income(String page,String rows,HttpServletRequest request){
	    Map<String, Object> retD = new HashMap<String, Object>();
	    
    	PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
        List<HashMap<String,Object>> list = reportService.income();
        PageInfo pageObj = new PageInfo(list);
        retD.put("total", pageObj.getTotal());
        retD.put("rows", list); 	
	    return retD;
	}

	@RequestMapping(value = "/cost")
	@ResponseBody
	public Object cost(String page,String rows,HttpServletRequest request){
	    Map<String, Object> retD = new HashMap<String, Object>();
	    
    	PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
        List<HashMap<String,Object>> list = reportService.cost();
        PageInfo pageObj = new PageInfo(list);
        retD.put("total", pageObj.getTotal());
        retD.put("rows", list); 	
	    return retD;
	}
	
	@RequestMapping(value = "/totalData", method = RequestMethod.GET)
    @ResponseBody
    public Object totalData() throws Exception{
		BaseResult rt = null;
    	try{
    		rt = reportService.totalData();
    	}catch(Exception e){
    		rt=new BaseResult("0","系统异常");
    		e.printStackTrace();
    	}
        return rt;
    }
	
	@RequestMapping(value = "/personTotal")
    public ModelAndView personTotal(HttpServletRequest request){
		return new ModelAndView("main/oareport/personTotal");
    }
	
	@RequestMapping(value = "/personIncome")
	@ResponseBody
	public Object personIncome(String page,String rows,HttpServletRequest request){
	    Map<String, Object> retD = new HashMap<String, Object>();	    
    	PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
        List<HashMap<String,Object>> list = reportService.personIncome();
        PageInfo pageObj = new PageInfo(list);
        retD.put("total", pageObj.getTotal());
        retD.put("rows", list); 	
	    return retD;
	}
	
	
	@RequestMapping(value = "/personPayment")
	@ResponseBody
	public Object personPayment(String page,String rows,HttpServletRequest request){
	    Map<String, Object> retD = new HashMap<String, Object>();	    
    	PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
        List<HashMap<String,Object>> list = reportService.personPayment();
        PageInfo pageObj = new PageInfo(list);
        retD.put("total", pageObj.getTotal());
        retD.put("rows", list); 	
	    return retD;
	}
}
